package com.powernode.controller;

import com.powernode.framework.Result;
import com.powernode.framework.ResultBean;
import com.powernode.framework.ResultUtil;
import com.powernode.pojo.DormitoryAdmin;
import com.powernode.pojo.FloorAdmin;
import com.powernode.pojo.IsolatedMan;
import com.powernode.service.DormitoryAdminService;
import com.powernode.service.FloorAdminService;
import com.powernode.service.IsolatedManService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: ycc
 * @Date: 2023/2/14 12:16
 */
@RestController
@RequestMapping("/api/isolatedMan")
public class IsolatedManController {
    @Autowired
    IsolatedManService isolatedManService;//隔离者
    @Autowired
    DormitoryAdminService dormitoryAdminService;
    @Autowired
    FloorAdminService floorAdminService;

    @PostMapping("/save")
    public Result<String> modify(@RequestBody IsolatedMan isolatedMan) {
        if (isolatedMan.getIsolatedBeginTime().getTime()<isolatedMan.getHaveIllTime().getTime()){
            return ResultUtil.fail("开始隔离日期，应在发现病情之后！");
        }
        if (isolatedMan.getRole().equals("寝室长")){ //如果是寝室长，判断一下该寝室是否已有寝室长
            DormitoryAdmin dormitoryAdmin = dormitoryAdminService.selectBySite(isolatedMan.getSite());
            //如果原来这个寝室已经有寝室长，并且这个寝室长不是被修改的这个隔离者
            if (dormitoryAdmin!=null && !(isolatedManService.selectByStudentNumber(dormitoryAdmin.getStudentNumber()).getId()).equals(isolatedMan.getId())){
                return ResultUtil.fail("此寝室已经有寝室长，请修改角色！");
            }
        }
        if (isolatedMan.getRole().equals("楼层长")){ //如果是楼层长，判断一下该层是否已有楼层长
            FloorAdmin floorAdmin = floorAdminService.selectByFloorSite(isolatedMan.getSite().substring(0,5));
            //如果原来这个楼层已经有楼层长，并且已有的这个楼层长不是被修改这个隔离者
            if (floorAdmin!=null && !floorAdmin.getStudentNumber().equals(isolatedMan.getStudentNumber())){
                return ResultUtil.fail("此楼层已经有楼层长，请修改角色！");
            }
        }
        isolatedManService.modify(isolatedMan);
        return ResultUtil.ok();
    }

    @PostMapping("/remove")
    public Result<String> remove(@RequestBody Integer[] ids) {
        isolatedManService.removes(ids);
        return ResultUtil.ok();
    }

    @GetMapping("/list")
    public ResultBean<List<IsolatedMan>> getPageResult(
            @RequestParam(required = false) String name,
            @RequestParam(required = false) String site,
            @RequestParam(required = false) String role,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer limit) {
        Map<String, Object> param = new HashMap<>(16);
        int offset = (page - 1) * limit;
        int rows = limit;
        param.put("name", name);
        param.put("site", site);
        param.put("role", role);
        param.put("offset", offset);
        param.put("rows", rows);
        int totalRows = isolatedManService.count(param);
        List<IsolatedMan> entities = isolatedManService.getPageResult(param);
        return new ResultBean<>(0, "查询成功", totalRows, entities);
    }
}